use std::{os::unix::process::CommandExt, process::Command};

#[tauri::command]
pub async fn check_is_installed(pkgname: String) -> Result<bool, String> {
    let output = Command::new("/opt/durapps/spark-store/bin/store-helper/check-is-installed")
        .arg(&pkgname)
        .output()
        .map_err(|e| format!("执行命令失败: {}", e))?;

    Ok(output.status.success())
}

#[tauri::command]
pub async fn check_launch_app(pkgname: String) -> Result<bool, String> {
    let output = Command::new("/opt/durapps/spark-store/bin/store-helper/ss-launcher")
        .arg("check")
        .arg(&pkgname)
        .output()
        .map_err(|e| format!("启动应用失败: {}", e))?;

    Ok(output.status.success())
}

#[tauri::command]
pub async fn launch_launch_app(pkgname: String) -> Result<(), String> {
    Command::new("/opt/durapps/spark-store/bin/store-helper/ss-launcher")
        .arg("launch")
        .arg(&pkgname)
        .process_group(0)
        .spawn()
        .map_err(|e| format!("启动应用失败: {}", e))?;

    Ok(())
}
